#include "epit.h"
#include "int.h"
#include "led.h"

void EPIT1_Init(void)
{
    // 1. 关闭EPIT1模块 bit[0]:0
    EPIT1->CR &= ~(0x1);

    // 2. 选取EPIT定时器的时钟源为IPG_CLK bit[25:24]
    EPIT1->CR &= ~(0x3<<24);
    EPIT1->CR |= (0x1<<24);

    // 3. 设置分频系数为1分频 bit[15:4]
    EPIT1->CR &= ~(0xFFF<<4);

    // 4. 设置EPIT的工作模式为自动重载模式(set-and-forget) bit[3]
    EPIT1->CR |= (0x1<<3);

    // 5. 设置初始值来源为EPIT1_LR寄存器 bit[1]
    EPIT1->CR |= (0x1<<1);

    // 6. 使能比较中断 bit[2]
    EPIT1->CR |= (0x1<<2);

    // 7. 配置LR寄存器的值为66000000/2=33000000
    EPIT1->LR=33000000;

    // 8. 配置CMPR寄存器的值
    EPIT1->CMPR=0;

    // 9.注册中断向量表
    BindIRQTable(EPIT1_IRQn,EPIT1_IRQhandler,NULL);

    // 10. 使能GIC
    GIC_EnableIRQ(EPIT1_IRQn);

    // 11.开启EPIT1模块
     EPIT1->CR |= (0x1);
}

void EPIT1_IRQhandler(unsigned int irq_id,void* para)
{
    static unsigned char state=0;
    state = !state;
    if((EPIT1->SR)&(0x1<<0))
    {
        Led_Toggle(state);
    }
    EPIT1->SR |= (0x1<<0);
}